﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RentalIService;

namespace RentalManagement.Controllers
{
    public class RoleController : Controller
    {
        // GET: Role

        //IOC构造函数调用[第二种调用方式]
        private readonly IServiceRole _IServiceRole;
        public RoleController(IServiceRole IServiceRole)
        {
            _IServiceRole = IServiceRole;
        }


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowRoleList(int offset, int limit, string sort, string order,string name)
        {
            int count = 0;   //总数据量
            if (String.IsNullOrWhiteSpace(sort))
            {
                sort = "Id";
            }
            var lst = _IServiceRole.ShowRoleList(offset, limit, sort, order, name, ref count);

            return Json(new { total = count, rows = lst }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult Add(string name, string index)
        {
            if (index == "")
            {
                return Json(_IServiceRole.Add(name), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(_IServiceRole.up(name,index), JsonRequestBehavior.AllowGet);
            }

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(string id)
        {
            var row = id.Split(',');
            int a = 0;
            foreach (var item in row)
            {
                if (_IServiceRole.Del(id))
                {
                    a += 1;
                }
            }
            if (a == row.Length)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }
    }
}